/*
 * Copyright (C) 2024 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.wakala.employment.fragment.login;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.wakala.employment.R;
import com.wakala.employment.activity.MainActivity;
import com.wakala.employment.adapter.entity.LoginInfo;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentLoginUniBinding;
import com.wakala.employment.fragment.other.LoginFragment;
import com.wakala.employment.utils.RandomUtils;
import com.wakala.employment.utils.TokenUtils;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.utils.ThemeUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.app.ActivityUtils;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class LoginUniFragment extends BaseFragment<FragmentLoginUniBinding>  implements View.OnClickListener{

    @NonNull
    @Override
    protected FragmentLoginUniBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentLoginUniBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle()
                .setImmersive(true);
        titleBar.setBackgroundColor(Color.TRANSPARENT);
        titleBar.setTitle("");
        titleBar.setLeftImageDrawable(ResUtils.getVectorDrawable(getContext(), R.drawable.ic_login_close));
        titleBar.setActionTextColor(ThemeUtils.resolveColor(getContext(), R.attr.colorAccent));
        return titleBar;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {
        binding.linkToMobileLogin.setOnClickListener(this);
        binding.buttonLogin.setOnClickListener(this);
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id==R.id.link_toMobileLogin){
            openPage(LoginFragment.class);
        }else if (id==R.id.button_login){
            XToastUtils.info("一键登录");
        }
    }



    /**
     * 登录成功的处理
     */
    private void onLoginSuccess(LoginInfo loginInfo) {
        String token = RandomUtils.getRandomNumbersAndLetters(16);
        if (TokenUtils.handleLoginSuccess(loginInfo)) {
            popToBack();
            ActivityUtils.startActivity(MainActivity.class);
        }
    }
}